<?php

namespace App\Admin\Controllers;

use App\Models\Major;
use App\Models\Student;
use Encore\Admin\Form;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Tree;
use Encore\Admin\Widgets\Box;
use Illuminate\Routing\Controller;
use Illuminate\Http\Request;

class MajorController extends Controller
{

    public function index(Content $content)
    {
        return $content
            ->header(trans('专业管理'))
            ->description(trans('专业列表'))
            ->row(function (Row $row) {
                $row->column(6, $this->treeView()->render());

                $row->column(6, function (Column $column) {
                    $form = new \Encore\Admin\Widgets\Form();
                    $form->action(admin_base_path('sm/major'));

                    $major = new Major();

                    $form->select('parent_id', trans('专业类别'))->options($major::selectOptions());
                    $form->text('code', trans('专业代码'))->rules('required');
                    $form->text('title', trans('专业名称'))->rules('required');
                    $form->date('formed', trans('成立时间'))->rules('required');
                    $form->hidden('_token')->default(csrf_token());

                    $column->append((new Box(trans('添加专业'), $form))->style('success'));
                });
            });
    }

    public function show($id)
    {
        return redirect('admin/sm/major');
    }

    protected function treeView()
    {
        $menuModel = 'App\Models\Major';

        return $menuModel::tree(function (Tree $tree) {

            $tree->disableCreate();
//            $tree->disableSave();
//            $tree->disableDel();

            $tree->branch(function ($branch) {
                $payload = "<strong>{$branch['code']}&nbsp;&nbsp;{$branch['title']}</strong>";

                if($branch['parent_id'] != 0){
                    $payload .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;现有学生人数： &nbsp;&nbsp; ";

                    $counts = Student::where('major_id', $branch['id'])->where('graduation', 0)->where('id', '>', '0')->count();

                    $payload .= $counts . ' 人';

                }
                return $payload;
            });
        });
    }

    public function edit($id, Content $content)
    {
        return $content
            ->header(trans('专业管理'))
            ->description(trans('修改专业信息'))
            ->row($this->form()->edit($id));
    }

    public function form()
    {
        $form = new Form(new Major);
        $major = new Major();

        $form->hidden('id', 'ID');

        $form->select('parent_id', trans('admin.parent_id'))->options($major::selectOptions());
        $form->text('code', trans('专业代码'))->rules('required');
        $form->text('title', trans('专业名称'))->rules('required');
        $form->date('formed', trans('成立时间'))->rules('required');
        $form->footer(function ($footer) {
            $footer->disableViewCheck();
            $footer->disableEditingCheck();
            $footer->disableCreatingCheck();
        });

        return $form;
    }

    function store(Request $request) {
        $major = new Major;
        $major->parent_id = $request->parent_id;
        $major->code = $request->code;
        $major->title = $request->title;
        $major->formed = $request->formed;
        $major->save();
    }

    function update(Request $request) {
        $major = Major::find($request->id);
        $major->parent_id = $request->parent_id;
        $major->code = $request->code;
        $major->title = $request->title;
        $major->formed = $request->formed;
        $major->save();
    }

    function 	destroy(Request $request) {
        $major=Major::find($request->major);
        $major->delete();
    }
}
